public class LC_707 {
}
class MyLinkedList {

    class ListNode{
        int val;
        ListNode next;
        ListNode(int val){
            this.val = val;
        }
    }

    private int size;
    private ListNode head; //虚拟头节点

    public MyLinkedList() {
        this.size = 0;
        this.head = new ListNode(0);
    }

    public int get(int index) {
        ListNode node = head;
        if(index >= size)return -1;
        while(index != -1){
            node = node.next;
            index--;
        }
        return node.val;
    }

    public void addAtHead(int val) {
        ListNode node = new ListNode(val);
        size++;
        node.next = head.next;
        head.next = node;
    }

    public void addAtTail(int val) {
        ListNode newNode = new ListNode(val);
        ListNode node = head;
        while(node.next != null){
            node = node.next;
        }
        node.next = newNode;
        size++;
    }

    public void addAtIndex(int index, int val) {
        if(index > size){
            return ;
        }
        ListNode newNode = new ListNode(val);
        ListNode node = head;
        while(index != 0){
            node = node.next;
            index--;
        }
        newNode.next = node.next;
        node.next = newNode;
        size++;

    }

    public void deleteAtIndex(int index) {
        if(index >= size)return;
        ListNode node = head;
        while(index != 0){
            node = node.next;
            index--;
        }
        if(node.next != null){
            node.next = node.next.next;
        }else{
            node.next = null;
        }
        size--;

    }
}
